package gen.tools.modUnit;

import gen.tools.Widgets;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class IsModUnitDuplicated extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public IsModUnitDuplicated() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		
		final Widgets wid = new Widgets();
		request.setCharacterEncoding( wid.UIcodeSet );
		response.setCharacterEncoding( wid.UIcodeSet );
		response.setContentType("application/text");
		
		String isModDuplicated = Boolean.toString(false);
		
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		try {
			conn = wid.getConn();
			ps = conn.prepareStatement("SELECT COUNT(*) FROM modUnits WHERE modUnitId=? AND subsysId=?");
			ps.setString(1, request.getParameter("modId"));
			ps.setString(2, request.getParameter("subsysId"));
			rs = ps.executeQuery();
			rs.next();
			if (rs.getInt(1)!=0 ) {
				isModDuplicated = Boolean.toString(true);
			}
		} catch(Exception e) {
			isModDuplicated = wid.errProcess(e);
			wid.errLog(e);
		} finally {
			try {
				if ( rs !=null ) {rs.close();}
				if ( ps !=null ) {ps.close();}
				if ( conn !=null ) {conn.close();}	
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		response.getWriter().write( isModDuplicated );
	}
}
